<?php
abstract class Epic_Form_Element_Abstract
{
    /**
     * Element attributes
     * @var array
     */
    protected $_attribs = array();
    
    /**
     * Html render
     * @var string
     */
    protected $_html;
    
    /**
     * array information decorator for element
     * @var array
     */
    protected $_decorators = array();
    
    /**
     * Epic_Validate_Element object
     * @var Epic_Validate_Element
     */
    protected $_validator = null;
    
    /**
     * Contructor
     * 
     * Set data attribute for element
     *
     * @param  array $data
     * @return void
     */
    public function __construct(array $data = array())
    {
		$this->setData($data);
    }
    
    /**
     * Set element attribute
     *
     * @param  string $key attribute name
     * @param  mixed $value attribute value
     * @return Epic_Form_Element_Abstract
     */
    public function setAttrib($key, $value)
    {
    	$this->_attribs[$key] = $value;
    	return $this;
    }
    
    /**
     * Retrive element attribute
     *
     * @param  string $key attribute name
     * @return string attribute value
     */
    public function getAttrib($key)
    {
    	if (!isset($this->_attribs[$key])) {
    		return null;
    	}
    	return $this->_attribs[$key];
    }
    
    /**
     * Set element data
     *
     * @param  string $data
     * @return Epic_Form_Element_Abstract
     */
    public function setData($data)
    {
   		if(is_array($data))
    		$this->_attribs = $data;
   		return $this;
    }
    
    /**
     * Set form id
     *
     * @param  string $id value for id attribute
     * @return Epic_Form_Element_Abstract
     */
    public function setId($value)
    {
    	return $this->setAttrib('id', (string)$value);
    }
    
    /**
     * Set form name
     *
     * @param  string $value value for name attribute
     * @return Epic_Form_Element_Abstract
     */
    public function setName($value)
    {
    	return $this->setAttrib('name', (string)$value);
    }
    
    /**
     * Set form value
     *
     * @param  string $value value for value attribute
     * @return Epic_Form_Element_Abstract
     */
    public function setValue($value)
    {
    	return $this->setAttrib('value', (string)$value);
    }
    
    /**
     * Retrive form value
     *
     * @return string|null value for value attribute
     */
    public function getValue()
    {
    	return $this->getAttrib('value');
    }
    
    /**
     * Set form title
     *
     * @param  string $value value for title attribute
     * @return Epic_Form_Element_Abstract
     */
    public function setTitle($value)
    {
    	return $this->setAttrib('title', (string)$value);
    }
    
    /**
     * Set form class
     *
     * @param  string $value value for class attribute
     * @return Epic_Form_Element_Abstract
     */
    public function setClass($value)
    {
    	return $this->setAttrib('class', (string)$value);
    }
    
    /**
     * Set Label for Element
     *
     * @param  string $label
     * @param  boolean $for
     * @return Epic_Form_Element_Abstract
     */
    public function setLabel($label, $for = null)
    {
    	$this->addDecorator('Helper', array('text' => '<label for="' . $for .'">' . $label . '</label>'));
    	return $this;
    }
    
    /**
     * Retrive Epic_Validate_Element_Abstract
     *
     * @return Epic_Validate_Element_Abstract
     */
    public function getValidator()
    {
    	return $this->_validator;
    }
    
    /**
     * Add a validator
     *
     * @param Epic_Validate_Element_Abstract $validator
     * @return Epic_Form_Element_Abstract
     */
    public function addValidator(Epic_Validate_Element_Abstract $validator)
    {
    	if(is_null($this->_validator))
    		$this->_validator = new Epic_Validate_Element();
    	
    	// add each element validator
    	$this->_validator->addValidator($validator);
    	return $this;
    }
    
    /**
     * Add a decorator for element
     *
     * @param  string $decoratorClass
     * @param  array $options
     * @return Epic_Form_Element_Abstract
     */
    public function addDecorator($decoratorClass, $options = null)
    {
   		if (is_string($decoratorClass)) {
        	$className = 'Epic_Form_Decorator_' . $decoratorClass;
        	$this->_decorators[] = array('name' => $className, 'options' => $options);
        }
        return $this;
   	}
   	
    /**
     * A decorators for element
     * 
     * @param array $data
     * @return Epic_Form_Element_Abstract
     */
    public function addDecorators(array $decorators)
    {
        if ($decorators) {
            foreach ($decorators as $decorator) {
                $this->addDecorator(key($decorator), current($decorator));
            }
        }
        return $this;
    }
    
    /**
     * Retrive all decorator for element
     *
     * @return void
     */
    public function getDecorators()
    {
    	$decorators ='';
        foreach ($this->_decorators as $decorator) {
        	$className = $decorator['name'];
        	$options = $decorator['options'];
        	$decoratorObj = new $className($this, $options);
            $this->_html = $decoratorObj->render();
        }
  	}
  	
  	/**
  	 * Retrive Content Html Element
  	 *
  	 * @return string
     */
   	public function getContent()
  	{
   		return $this->_html;
   	}  
}